package com.data.monitoring.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.monitoring.model.entity.FolderStatus;
import com.data.monitoring.service.FolderStatusService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/status")
@Api(tags = "监听任务状态控制器")
public class StatusController {
    
    @Autowired
    private FolderStatusService statusRepository;
    
    /**
     * 获取最新状态
     */
    @GetMapping("/latest")
    public ResponseEntity<List<FolderStatus>> getLatestStatus() {
        List<FolderStatus> latestStatus = statusRepository.findLatestStatus();
        return ResponseEntity.ok(latestStatus);
    }
    
    /**
     * 获取历史记录
     */
    @GetMapping("/history/{folderId}")
    public ResponseEntity<Page<FolderStatus>> getHistory(
            @PathVariable Long folderId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "50") int size) {
        
        Pageable pageable = PageRequest.of(page, size, Sort.by("checkTime").descending());
        Page<FolderStatus> history = statusRepository.findByFolderId(folderId, pageable);
        
        return ResponseEntity.ok(history);
    }
}